<?php
include_once "function_xml_atom.php";
include_once "function_sys_license.php";

function GetOnlyOneValue($query_string)
{
        $results = GetAllNodeValue($query_string);
	$attributes = GetAllNodeValue($query_string);
	
	if ($attributes->item(0)->getAttribute('country') == 'cn')
		return base64_decode($results->item(0)->nodeValue);
       	else
		return $results->item(0)->nodeValue;
}

function GetOneNodeByAttr($query_string,$Attr_Name,$Attr_Value)
{
        $results = GetAllNodeValue($query_string. "[@". $Attr_Name. "='". $Attr_Value. "']");
	
	return $results;
}

function GetOneNodeByNumber($query_string,$Number)
{
        $results = GetAllNodeValue($query_string. "[". ($Number). "]");
	print_r($results);
	//$result = $results->item[0];
	return $results;
}

#$query_string = "/MINI/NETWORK/INTERFACES/INTERFACE";
function GetNumberByAttribute($query_string, $attribute, $value)
{
        $number = GetNumberOfNode($query_string);
	
	for ($i = 1; $i < $number; $i ++)
	{
		$query_string_real = $query_string. "[". $i. "]";
		$result = GetAllNodeValue($query_string_real)->item(0);
		if ($value == $result->getAttribute($attribute))
		{
			return $i;
		}
	}
	return 0;
}
# $num = GetNumberByAttribute($query_string, 'Name', 'eth8');
# print $num . "\n"; 

function AppendAllAttrOfNode($query_string, $node_name, $Valu_array)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];
	
        $results = $xpath->evaluate($query_string);
        $result = $results->item(0);

        $new_node = $doc->createElement($node_name);

	$Attr_array = array_keys($Valu_array);

	foreach ($Attr_array as $Attr)
 	{
        	$new_node->setAttribute($Attr, $Valu_array[$Attr]);
	}
        $result->appendChild($new_node);

	if (CheckSysLicense2() == TRUE)
        	$doc->save(XMLFILE);
}

function AppendAllAttrOfNodeList($query_string, $node_name, $Vlist)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];
	
        $results = $xpath->query($query_string);
        $result = $results->item(0);
	
	foreach($Vlist as $Value_array){
		$new_node = $doc->createElement($node_name);
		$Attr_array = array_keys($Value_array);
		foreach ($Attr_array as $Attr)
		{
			$new_node->setAttribute($Attr, $Value_array[$Attr]);
		}
		$result->appendChild($new_node);
	}

	if (CheckSysLicense2() == TRUE)
        	$doc->save(XMLFILE);
}

function EditAllAttrOfNode($query_string, $Valu_array)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];
	
        $results = $xpath->query($query_string);
	$result = $results->item(0);	

	$Attr_array = array_keys($Valu_array);
	foreach ($Attr_array as $Attr)
        {	
                $result->setAttribute($Attr, $Valu_array[$Attr]);
        }
	if (CheckSysLicense2() == TRUE)
        	$doc->save(XMLFILE);
}

function EditAllAttrOfNodeList($query_string, $Vlist)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];
        $results = $xpath->query($query_string);

	$i = 0;
	foreach ($Vlist as $Valu_array){
		$result = $results->item($i);	
		$Attr_array = array_keys($Valu_array);
		foreach ($Attr_array as $Attr)
		{	
			$result->setAttribute($Attr, $Valu_array[$Attr]);
		}
		$i ++;
	}	
	if (CheckSysLicense2() == TRUE)
        	$doc->save(XMLFILE);	
}

function DelNodeByAttr($query_string, $Attr_Name, $Attr_Value)
{
	//DeleteNode($query_string. "[@". $Attr_Name. "='". $Attr_Value. "']");
	DelSpecialNode($query_string, $Attr_Value, $Attr_Name);
}

#$query_string= "/MINI/NETWORK/DNSS/DNS";
function DelSpecialNode()
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$arg_num = func_num_args();
	if ($arg_num >= 1)
		$query_string = func_get_arg(0);
	if ($arg_num >= 2){
		$node_value = func_get_arg(1);
	}
	if ($arg_num >= 3){
		$node_attr_name = func_get_arg(2);
	}

	$parent = $xpath->query($query_string);
	foreach($parent as $node)
	{	
		if ($arg_num == 1){
			$node->parentNode->removeChild($node);
		}
		if ($arg_num == 2){
			if ($node->nodeValue == $node_value){
				$node->parentNode->removeChild($node);
			}
		}
		if ($arg_num == 3){
			if ($node-> getAttribute($node_attr_name) == $node_value)
				$node -> parentNode ->removeAttribute($node);
		}
	}

	if (CheckSysLicense2() == TRUE)
		$doc->save(XMLFILE);
}
#DeleteNode($query_string, $node_value, $node_attr_name);

function MoveFrontOneNode($query_string_from, $query_string_to)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results_from = $xpath->query($query_string_from);
        $result_from = $results_from->item(0);
        
	$results_to= $xpath->query($query_string_to);
        $result_to= $results_to->item(0);

        $result_to->parentNode->insertBefore($result_from,$result_to);

	if (CheckSysLicense2() == TRUE)
        	$doc->save(XMLFILE);
}
#$query_string_from = "/MINI/NETWORK/ROUTES/POLICY/ROUTE[1]";
#$query_string_to = "/MINI/NETWORK/ROUTES/POLICY/ROUTE[4]";
#MoveFrontOneNode($query_string_from, $query_string_to);

function MoveEndOneNode($query_string_from, $query_string_to)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results_from = $xpath->query($query_string_from);
        $result_from = $results_from->item(0);
        
	$results_to= $xpath->query($query_string_to);
        $result_to= $results_to->item(0);

        $result_to->parentNode->insertBefore($result_from);

	if (CheckSysLicense2() == TRUE)
        	$doc->save(XMLFILE);
}

# The number from one start.
function InsertOneNode($query_string_from, $node_name, $Valu_array)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
        $doc = $Ret_Array["doc"];
        $xpath = $Ret_Array["xpath"];

        $results_from = $xpath->query($query_string_from);
        $result_from = $results_from->item(0);

        $new_node = $doc->createElement($node_name);
        $Attr_array = array_keys($Valu_array);
        foreach ($Attr_array as $Attr)
        {       
                $new_node->setAttribute($Attr, $Valu_array[$Attr]);
        } 

	$result_from->parentNode->insertBefore($new_node, $result_from); 
	if (CheckSysLicense2() == TRUE)
        	$doc->save(XMLFILE);
}

function GetAttributList($query_string, $in_array)
{
        $key_array = $in_array;
	$ret_array = array ();
	$results = GetAllNodeValue($query_string);
        $i = 0;
        foreach ($results as $result)
        {
		foreach ($key_array as $key)
		{
			$ret_array[$i][$key] = $result->getAttribute($key);
		}
		$i ++;
	}
	return $ret_array;
}

function CheckChildNodeExist($xpath, $path, $ChildName)
{
	$roots = $xpath->query($path);

	foreach ($roots as $root)
	{	
		$ret = $root->hasChildNodes();
		if ($ret)
		{
			$childs = $root->childNodes;
			foreach ( $childs as $child)
			{
				if ($child -> nodeName == $ChildName)
				{
					return TRUE;
				}	
			}
		}
	}
	return FALSE;
}

#$query_string= "/MINI/NETWORK/DNSS";
function AppendUniqueEle($doc, $xpath, $query_string, $ele_name)
{
	$results = $xpath->query($query_string);
	if ($results ->length == 0){
		return FALSE;
	}
	$result = $results->item(0);

	$child_list = $result -> childNodes;
	if ($child_list ->length > 0){
		foreach ( $child_list as $r){
			if ($r -> nodeName == $ele_name){
				return TRUE;
			}	
		}
	}

	$new_ele = $doc->createElement($ele_name);
	$result->appendChild($new_ele);

	return TRUE;
}

#$query_string = "/MINI/NETWORK/DNSS/DNS";
function CheckNodeExist($query_string)
{
	$Ret_Array = OpenXmlFileForWrite(XMLFILE);
	$doc = $Ret_Array["doc"];
	$xpath = $Ret_Array["xpath"];

	$list = explode ("/",$query_string);
	$i = 0;
	$path = "";
		
	foreach ( $list as $l)
	{	
		$i++;
		$lcount = count($list);
		if ($lcount == $i){
			break;
		}
		$len = strlen($path);
		if ($path[$len - 1] != "/") {
			$path = $path. "/". $l ;
		}
		else{
			$path = $path. $l ;
		}
		$str = $list[$i];

		if (!CheckChildNodeExist($xpath, $path, $str)){
			AppendUniqueEle($doc, $xpath, $path, $str);
		}	
	}	
	$ret = $doc->save(XMLFILE);
}
#CheckNodeExist($query_string);

?>
